package com.servertrack

import com.commonProject.common.classes.RequestDTO
import com.commonProject.common.exception.ValidateException
import com.commonProject.common.intefaces.IRequest

import grails.transaction.Transactional
import org.springframework.context.MessageSource


class RequestService implements IRequest{
	
	static transactional=true
	MessageSource messageSource
	
	List<RequestDTO> getlist(Long request_id){
		List<RequestDTO> dtolist=new ArrayList<RequestDTO>()
		RequestDTO dto=null
		
		Times requestTo=Times.get(request_id)
		
		List<Request>req=Request.findAllByTimes(requestTo)		  
		req.each{
			dto=new RequestDTO()
			dto.id=("Request_"+it.id)
			dto.title=it.title
			dtolist<<dto
		}		
		
		return dtolist
	}
	
	RequestDTO getelement(Long request_id){
		println "SERVER REQUEST GETELEMENT"
		
		RequestDTO dto = new RequestDTO()
		def el=Request.get(request_id)
		
		dto.id=el.id;
		dto.title=el.title;
		dto.phone_number=el.phone_number;
		dto.company_name=el.company_name;
		dto.count_persons=el.count_persons;
		dto.description=el.description;
		dto.birthDate=el.birthDate;
		dto.name_tourist=el.name_tourist;
		
		return dto
	}
	
	RequestDTO deleteElement(Long request_id){
		def el=Request.get(request_id)
		el.delete()
	}
	
	public void addElement(Long r_id,String r_title,String r_company_name,Integer r_count_persons,String r_name_tourist,Long r_phone_number,String r_description) throws ValidateException{		
		def time=Times.get(r_id)
		println("time = "+time)		
		println("Add Request SERVER*******")
		
		if (time) {
			println("block TIME")
			def newRequest=new Request()
			newRequest.title=r_title
			newRequest.company_name=r_company_name
			newRequest.count_persons=r_count_persons
			newRequest.name_tourist=r_name_tourist
			newRequest.phone_number=r_phone_number
			newRequest.description=r_description
			newRequest.times=time
			
			newRequest.birthDate=new Date()				
	
			println("AFTER addToRequest")
						
	
			if (!newRequest.save()){			
				def err = newRequest.errors.allErrors.collect{[
					errorMessage:messageSource.getMessage(it, null)			
					]}
								throw new ValidateException(err)
				}			
			}		
		}			
	
	public void editElement(Long r_id,String r_title,String r_company_name,Integer r_countPersons,String r_touristName,Integer r_phoneNumber,String r_description, String r_birthDate) throws  ValidateException {
		println("Edit Request SERVER")
		def req=Request.get(r_id)
		req.title=r_title
		req.company_name=r_company_name
		req.count_persons=r_countPersons
		req.name_tourist=r_touristName
		req.phone_number=r_phoneNumber
		req.description=r_description
		
		if (r_birthDate){
			req.birthDate=new Date(r_birthDate)
		}else{
			req.birthDate=null
		}
		
		if (!req.save()){			
				def err = req.errors.allErrors.collect{[
					errorMessage:messageSource.getMessage(it, null)						
				]}
				throw new ValidateException(err)
		}	
	}
	

	}
